package com.whlilong.jxc.dialog;

import java.util.List;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;

import com.swtdesigner.ResourceManager;
import com.whlilong.jxc.db.DbManager;
import com.whlilong.jxc.model.Depart;
import com.whlilong.jxc.model.Users;
import com.whlilong.jxc.sys.ExtentionInfo;
import com.whlilong.jxc.sys.SaveLogInfo;

import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Combo;

public class UserManagerDialog extends AbstractDialog {

	private String username;
	private Text textOpass;
	private Text textNpass;
	private Text textCpass;
	private Text textusername;
	private Text textpass;
	private Combo comboUsername;
	private Combo combotype;
	private CTabFolder tabFolder;
	private List<Users> list = ExtentionInfo.getInstance().getUser();
	private Text textcpass;
	private boolean isSuccessful = false;
	private boolean isadd = false;
	private Combo combo;
	private boolean isdelete = false;

	public UserManagerDialog(Shell parent, String username) {
		// TODO Auto-generated constructor stub
		super(parent);
		this.username = username;
	}

	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("用户管理");
	}

	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, "确定(&S)", true);
		createButton(parent, IDialogConstants.CANCEL_ID, "取消(&X)", false);
	}

	public Control createGroupText(Composite parent) {
		GridLayout gl_parent = new GridLayout(1, false);
		gl_parent.marginTop = 20;
		gl_parent.marginRight = 5;
		gl_parent.marginBottom = 20;
		gl_parent.marginLeft = 40;
		parent.setLayout(gl_parent);

		tabFolder = new CTabFolder(parent, SWT.BORDER);
		tabFolder.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true,
				false, 1, 1));
		tabFolder.setSimple(false);
		tabFolder.setSelectionBackground(Display.getCurrent().getSystemColor(
				SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT));

		CTabItem tabItem = new CTabItem(tabFolder, SWT.NONE);
		tabItem.setImage(ResourceManager.getPluginImage("com.whlilong.jxc",
				"icons/brand1.gif"));
		tabItem.setText("修改用户密码");

		Composite composite = new Composite(tabFolder, SWT.BORDER);
		tabItem.setControl(composite);
		GridLayout gl_composite = new GridLayout(2, false);
		gl_composite.marginRight = 100;
		gl_composite.verticalSpacing = 10;
		gl_composite.marginTop = 10;
		gl_composite.marginLeft = 50;
		gl_composite.marginBottom = 20;
		composite.setLayout(gl_composite);

		Label labelUsername = new Label(composite, SWT.NONE);
		labelUsername.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		labelUsername.setText("用户名：");

		comboUsername = new Combo(composite, SWT.READ_ONLY);
		GridData gd_comboUsername = new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1);
		gd_comboUsername.widthHint = 120;
		comboUsername.setLayoutData(gd_comboUsername);
		for (int i = 0; i < list.size(); i++)
			comboUsername.add(list.get(i).getUsername());
		comboUsername.select(0);

		Label labelOpass = new Label(composite, SWT.NONE);
		labelOpass.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		labelOpass.setText("原密码：");

		textOpass = new Text(composite, SWT.BORDER);
		textOpass.setEchoChar('*');
		textOpass.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		Label labelNpass = new Label(composite, SWT.NONE);
		labelNpass.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		labelNpass.setText("新密码：");

		textNpass = new Text(composite, SWT.BORDER);
		textNpass.setEchoChar('*');
		textNpass.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		Label labelCpass = new Label(composite, SWT.NONE);
		labelCpass.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		labelCpass.setText("确认密码：");

		textCpass = new Text(composite, SWT.BORDER);
		textCpass.setEchoChar('*');
		textCpass.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		CTabItem tabItem_1 = new CTabItem(tabFolder, SWT.NONE);
		tabItem_1.setText("添加新用户");

		Composite composite_1 = new Composite(tabFolder, SWT.NONE);
		tabItem_1.setControl(composite_1);
		GridLayout gl_composite_1 = new GridLayout(2, false);
		gl_composite_1.verticalSpacing = 20;
		gl_composite_1.marginTop = 20;
		gl_composite_1.marginRight = 100;
		gl_composite_1.marginLeft = 50;
		gl_composite_1.marginBottom = 20;
		composite_1.setLayout(gl_composite_1);

		tabFolder.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void widgetSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				if (tabFolder.getSelectionIndex() != 0
						&& !SaveLogInfo.getInstance().getUsertag().equals("0")) {
					MessageDialog.openError(null, "错误", "没有该权限！");
					tabFolder.setSelection(0);
				}
			}

		});

		Label labelusername = new Label(composite_1, SWT.NONE);
		labelusername.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		labelusername.setText("用户名：");

		textusername = new Text(composite_1, SWT.BORDER);
		textusername.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));

		Label labelpass = new Label(composite_1, SWT.NONE);
		labelpass.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		labelpass.setText("密码：");

		textpass = new Text(composite_1, SWT.BORDER);
		textpass.setEchoChar('*');
		textpass.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		Label label = new Label(composite_1, SWT.NONE);
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label.setText("确认密码：");

		textcpass = new Text(composite_1, SWT.BORDER);
		textcpass.setEchoChar('*');
		textcpass.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		Label labeltype = new Label(composite_1, SWT.NONE);
		labeltype.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		labeltype.setText("用户类型：");

		combotype = new Combo(composite_1, SWT.READ_ONLY);
		combotype.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		combotype.add("系统管理员");
		combotype.add("后台管理员");
		combotype.add("前台收银员");
		combotype.select(2);

		CTabItem tabItem_2 = new CTabItem(tabFolder, SWT.NONE);
		tabItem_2.setText("删除用户");

		Composite composite_2 = new Composite(tabFolder, SWT.NONE);
		tabItem_2.setControl(composite_2);
		GridLayout gl_composite_2 = new GridLayout(2, false);
		gl_composite_2.marginRight = 5;
		gl_composite_2.marginLeft = 5;
		gl_composite_2.marginTop = 30;
		composite_2.setLayout(gl_composite_2);

		Label label_1 = new Label(composite_2, SWT.NONE);
		label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label_1.setText("用户名：");

		combo = new Combo(composite_2, SWT.READ_ONLY);
		combo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
				1));
		for (int i = 0; i < list.size(); i++)
			combo.add(list.get(i).getUsername());
		combo.select(0);

		return parent;

	}

	protected void exeSearch() {
		String opass = textOpass.getText();
		String npass = textNpass.getText();
		String cpass = textCpass.getText();

		switch (tabFolder.getSelectionIndex()) {
		case 0:
			String username = comboUsername.getText();
			for (int i = 0; i < list.size(); i++) {
				if (list.get(i).getUsername().equals(username)) {
					if (!opass.equals(list.get(i).getPassword())) {
						MessageDialog.openError(null, "错误", "原始密码错误！");
						isSuccessful = false;
						return;
					} else {
						if (!npass.equals(cpass)) {
							MessageDialog.openError(null, "错误", "新密码与确认密码不一致！");
							isSuccessful = false;
							return;
						} else {
							Users user = list.get(i);
							user.setUsername(username);
							user.setPassword(npass);
							DbManager.getInstance().update(user);
							isSuccessful = true;
						}
					}
				}
			}
			break;
		case 1:
			String name = textusername.getText();
			String pass = textpass.getText();
			String cfgpass = textcpass.getText();
			int status = combotype.getSelectionIndex();
			if (name.equals("") || name == null) {
				MessageDialog.openError(null, "错误", "用户名不能为空！");
				isadd = false;
				return;
			}
			if (!pass.equals(cfgpass)) {
				MessageDialog.openError(null, "错误", "密码与确认密码不一致！");
				isadd = false;
				return;
			}
			for (int j = 0; j < list.size(); j++) {
				if (list.get(j).getUsername().equals(name)) {
					MessageDialog.openError(null, "错误", "用户名已存在！");
					isadd = false;
					return;
				}
			}
			Users users = new Users();
			users.setUsername(name);
			users.setPassword(pass);
			users.setUsertag(String.valueOf(status));
			DbManager.getInstance().insert(users);
			isadd = true;
			break;
		case 2:
			String uname = combo.getText();
			for (int i = 0; i < list.size(); i++) {
				if (list.get(i).getUsername().equals(uname)) {
					if (MessageDialog.openConfirm(null, "删除用户", "确定删除该用户？")) {
						DbManager.getInstance().delete(list.get(i));
						isdelete = true;
						break;
					}
				}
			}
			break;
		}

	}

	protected void okPressed() {
		if (isSuccessful) {
			MessageDialog.openInformation(null, "恭喜", "恭喜，修改密码成功！");
			setReturnCode(OK);
			close();
		}
		if (isadd) {
			MessageDialog.openInformation(null, "恭喜", "恭喜，添加用户成功！");
			setReturnCode(OK);
			close();
		}
		if (isdelete) {
			MessageDialog.openInformation(null, "恭喜", "恭喜，删除用户成功！");
			setReturnCode(OK);
			close();
		}
	}
}
